package com.sky.cqculture.Contraller;

import com.sky.cqculture.pojo.Result;
import com.sky.cqculture.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class Uploadcontraller {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    //本地上传文件
//    @PostMapping("/upload")
//    public Result upload(String name, Integer age, MultipartFile file) throws IOException {//MultipartFile是上传的文件，不然上传的是文件名字
//        System.out.println("name="+name+",id="+age+",file="+file);
//
//        String OriginalFilename = file.getOriginalFilename();
//        String extension=OriginalFilename.substring(OriginalFilename.lastIndexOf("."));//获取文件后缀,通过"."分割字符串
//        String newFileName = UUID.randomUUID().toString()+extension;//生成随机文件名通过uuid生成随机文件名
//        //保存文件
//        file.transferTo(new File("E:/前端/图片/"+newFileName));
//        return Result.success();
//    }



    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        System.out.println("文件file="+file.getOriginalFilename());
        //将文件交过于阿里云
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());//文件内容和文件名
        System.out.println("文件上传oos的url="+url);
        return Result.success(url);
    }


}





















